local chenqing = fk.CreateSkill {

  name = "joy__chenqing",

  tags = {  },

}



chenqing:addEffect(fk.EnterDying, {
  name = "joy__chenqing",
  anim_type = "support",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(chenqing.name) and player:usedSkillTimes(chenqing.name, Player.HistoryTurn) == 0 and
    not table.every(player.room.alive_players, function (p)
      return p == player or p == target
    end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, p in ipairs(room.alive_players) do
      if p ~= target then
        table.insert(targets, p.id)
      end
    end
    if #targets == 0 then return end
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#joy__chenqing-choose", chenqing.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    to:drawCards(5, chenqing.name)
    local cards = room:askForDiscard(to, 4, 4, true, chenqing.name, false, ".", "#joy__chenqing-discard", true)
    local suits = {}
    for _, id in ipairs(cards) do
      if Fk:getCardById(id).suit ~= Card.NoSuit then
        table.insertIfNeed(suits, Fk:getCardById(id).suit)
      end
    end
    room:throwCard(cards, chenqing.name, to, to)
    if #suits == 4 and not to.dead and not target.dead then
      room:useVirtualCard("peach", nil, to, target, chenqing.name)
    end
  end,
})

return chenqing